<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="title">{{$t('achievements.title1')}}</div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- APP名称 -->
          <div class="searchItem">
            <span>{{$t('appHelpCenter.appName')}}</span>
            <el-input
              v-model="listQuery.appName"
              :placeholder="$t('creditSupermarket.ruleName')"
              size="small"
            ></el-input>
          </div>
          <div class="menu-title-btn" style="margin-left: 20px;">
            <button type="button" @click="search">{{$t('btn.query')}}</button>
          </div>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('DailyStatistics.ListDetails')}}</div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%; margin-top: 5px;"
        border
        stripe
        size="mini"
      >
        <!-- APP名称 -->
        <el-table-column
          prop="appName"
          align="center"
          header-align="center"
          :label="$t('appHelpCenter.appName')"
        ></el-table-column>
        <!-- 版本号 -->
        <el-table-column
          prop="verId"
          align="center"
          header-align="center"
          :label="$t('AppVersion.verId')"
        ></el-table-column>
        <!-- 渠道 -->
        <el-table-column
          prop="channelName"
          align="center"
          header-align="center"
          :label="$t('productManage.channels')"
        ></el-table-column>
        <!-- 订单前缀 -->
        <el-table-column
          prop="appShortName"
          align="center"
          header-align="center"
          :label="$t('AppVersion.OrderPrefix')"
        ></el-table-column>
        <!-- 创建时间 -->
        <el-table-column
          prop="crtTime"
          align="center"
          header-align="center"
          :label="$t('approvalDetail.smsCrtTime')"
        ></el-table-column>
        <!-- 发布状态 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="onlineStatus"
          :label="$t('AppVersion.PublishStatus')"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.onlineStatus === 0">{{$t("AppVersion.Stateless")}}</span>
            <span v-if="scope.row.onlineStatus === 1">{{$t("AppVersion.SuccessRelease")}}</span>
            <span v-if="scope.row.onlineStatus === 2">{{$t("AppVersion.PublishFail")}}</span>
            <span v-if="scope.row.onlineStatus === 3">{{$t("AppVersion.Undercarriage")}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('user.operation')" width="140">
          <template slot-scope="scope">
            <!-- 查看下载地址 -->
            <el-button size="small" type="text" @click="packageFn(scope.row)">
              {{$t('AppVersion.PackageManage')}}
            </el-button>
            <!-- <div>
              <div v-if="scope.row.uploadUrl !== ''">
                <a
                  @click="downLoading"
                  :href="scope.row.uploadUrl"
                  style="color:#20a0ff;"
                  class="href"
                  target="_blank"
                  :plain="true"
                >{{$t('AppVersion.seeDownloadUrl')}}</a>
              </div>
              <div v-if="scope.row.uploadUrl === ''">
                <span class="href" :plain="true" @click="noPackage">{{$t('AppVersion.seeDownloadUrl')}}</span>
              </div>
            </div> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 查看下载地址 -->
    <el-dialog
      :title="$t('AppVersion.PackageManage')"
      :visible.sync="packageDialogVisible"
      width="30%"
      center>
      <div style="text-align: center;padding-bottom: 20px;">
        <!-- 未上传 -->
        <div v-if="isUpload === 0" style="padding: 20px 0;">
          {{$t("AppVersion.noDownloadUrlWarn")}}
        </div>
        <!-- 已上传 -->
        <div v-if="isUpload === 1">
          <el-form ref="downloadForm" :model="downloadForm">
            <el-form-item :label="$t('creditSupermarket.downloadAddress')" label-width="80px">
              <el-input size='small' v-model="downloadForm.httpUrl" :placeholder="$t('AppVersion.ruleUploadUrl')" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item>
              <!-- 复制下载地址 -->
              <el-button type="primary" @click="copyCode(downloadForm.httpUrl)">{{$t('AppVersion.copyDownloadUrl')}}</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { appManagePage } from "api/version/index";
export default {
  data() {
    return {
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        appName: "",
        accurateAppName: 1
      },
      tableData: [],
      isUpload: "",
      downloadForm: {
        httpUrl: ''
      },
      packageDialogVisible: false,
    };
  },
  created() {
    if (this.DownloadPackagePage.has(this.$route.path)) {
      this.listQuery = this.DownloadPackagePage.get(this.$route.path);
    }
    this.getList();
  },
  computed: {
    ...mapGetters(["elements", "DownloadPackagePage"])
  },
  methods: {
    // 查看下载地址
    packageFn(row) {
      this.downloadForm.httpUrl = row.httpUrl;
      if (row.httpUrl === '') { // 未上传
        this.isUpload = 0;
      } else if (row.httpUrl !== '') { // 已上传
        this.isUpload = 1;
      }
      this.packageDialogVisible = true;
    },
    // 复制下载地址
    copyCode(shareLink) {
      var input = document.createElement("input");     // 直接构建input
      input.value = shareLink;   // 设置内容
      document.body.appendChild(input);        // 添加临时实例
      input.select();      // 选择实例内容
      document.execCommand("Copy");     // 执行复制
      document.body.removeChild(input);  // 删除临时实例
      this.$message({
        showClose: true,
        message: this.$t('collDetail.copySuccess'),
        type: 'success'
      });
    },
    // 下载安装包提示
    // downLoading() {
    //   this.$message({
    //     showClose: true,
    //     message: this.$t("AppVersion.downloadWarn"),
    //     type: "success"
    //   });
    // },
    // 未上传包提示
    // noPackage() {
    //   this.$message({
    //     showClose: true,
    //     message: this.$t("AppVersion.noDownloadUrlWarn"),
    //     type: "warning"
    //   });
    // },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveDownloadPackagePage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      appManagePage(this.listQuery).then(response => {
        if (response.status === 200) {
          this.listLoading = false;
          this.tableData = response.data.rows;
          this.total = Number(response.data.total);
        } else {
          this.$message({
            showClose: true,
            type: "warning",
            message: response.message
          });
        }
      });
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 180px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 180px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.href {
  cursor: pointer;
  text-decoration: none;
  color: #20a0ff;
}
.hiddenInput {
  display: none;
}
</style>